﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UcAsp.Net.Extract.Controls
{
    public partial class TabButton : Control
    {
        Bitmap active = new Bitmap(Properties.Resources.active);
        Bitmap noative = new Bitmap(Properties.Resources.noactive);
        Bitmap overactive = new Bitmap(Properties.Resources.overactive);
        Bitmap close = new Bitmap(Properties.Resources.close);
        private bool _isactive = false;
        public bool IsActive { get { return this._isactive; } set { this._isactive = value; } }
        public TabButton()
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint
                   | ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
            OnDraw(this.CreateGraphics(), active);
        }
        #region 重写属性

        protected override CreateParams CreateParams
        {
            get
            {
                var result = base.CreateParams;
                result.ExStyle |= 0x00000020;
                return result;
            }
        }

        #endregion

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (IsActive)
            {
                OnDraw(pe.Graphics, active);
            }
            else
            {
                OnDraw(pe.Graphics, noative);
            }
           
        }
        protected  void OnDraw(Graphics g, Bitmap bitmap)
        {
            SizeF size = g.MeasureString(this.Text, this.Font);
            g.DrawImage(bitmap, 0, 0);
            float l = (this.Width - size.Width) / 2;
            float t = (this.Height - size.Height) / 2;
            this.Size=bitmap.Size;
            Brush b = new SolidBrush(Color.White);
            g.DrawString(this.Text, this.Font, b, l, t);
            g.Dispose();
        }
        public new event EventHandler Click;
        public event EventHandler Close;
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (e.X < 220 && e.Y < 25 && Click != null)
            {
                Click(this, EventArgs.Empty);
            }
            else
            {
                if (Close != null)
                {
                    Close(this, EventArgs.Empty);
                }
            }
        }
        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            if (IsActive)
            {
                OnDraw(this.CreateGraphics(), active);
            }
            else
            {
                OnDraw(this.CreateGraphics(), overactive);
            }
           
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (IsActive)
            {
                OnDraw(this.CreateGraphics(), active);
            }
            else
            {
                OnDraw(this.CreateGraphics(), noative);
            }
        }
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            Close(this, EventArgs.Empty);
        }
    }
}
